package com.weatherapp.simple.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.weatherapp.simple.R;
import com.weatherapp.simple.model.HourlyWeather;
import java.util.List;

public class HourlyWeatherAdapter extends RecyclerView.Adapter<HourlyWeatherAdapter.ViewHolder> {
    
    private List<HourlyWeather> hourlyWeatherList;

    public HourlyWeatherAdapter(List<HourlyWeather> hourlyWeatherList) {
        this.hourlyWeatherList = hourlyWeatherList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_hourly_weather, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        HourlyWeather weather = hourlyWeatherList.get(position);
        holder.tvTime.setText(weather.getTime());
        holder.tvWeatherIcon.setText(weather.getWeatherIcon());
        holder.tvTemperature.setText(weather.getTemperature() + "°C");
    }

    @Override
    public int getItemCount() {
        return hourlyWeatherList != null ? hourlyWeatherList.size() : 0;
    }

    public void updateData(List<HourlyWeather> newData) {
        this.hourlyWeatherList = newData;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvWeatherIcon;
        TextView tvTemperature;

        ViewHolder(View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvWeatherIcon = itemView.findViewById(R.id.tv_weather_icon);
            tvTemperature = itemView.findViewById(R.id.tv_temperature);
        }
    }
}